package com.basic.business.common.utils;

import lombok.extern.slf4j.Slf4j;

/**
 * 监控任务执行
 */
@Slf4j
public class MonitoredTask implements Runnable {
    private final Runnable originalTask;

    public MonitoredTask(Runnable originalTask) {
        this.originalTask = originalTask;
    }

    @Override
    public void run() {
        long startTime = System.currentTimeMillis();
        String taskName = originalTask.getClass().getSimpleName();

        try {
            log.info("任务 {} 开始执行", taskName);
            originalTask.run();
            log.info("任务 {} 执行完成，耗时 {} ms", taskName, System.currentTimeMillis() - startTime);
        } catch (Exception e) {
            log.error("任务 {} 执行异常", taskName, e);
        }
    }
}